package com.test.learnaidl.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.test.learnaidl.PersonMannageInterface;
import com.test.learnaidl.R;
import com.test.learnaidl.bean.PersonBean;
import com.test.learnaidl.service.PersonManageService;
import com.test.learnaidl.utils.ToastUtils;

import java.util.List;
import java.util.UUID;

/*
    AIDL-修饰符in,out,inout
    https://www.yuque.com/jiajun-faizt/haag2g/atiecz

    安卓开发学习之AIDL的使用 https://blog.csdn.net/qq_37475168/article/details/80580773   此篇文章 代码里的注解比较详细

    android进阶-AIDL的基本使用  https://www.jianshu.com/p/5043a1a69269

    Android AIDL使用介绍（1）基本使用 https://blog.csdn.net/lansoul1987/article/details/104350185

    Android AIDL 使用 https://blog.51cto.com/u_15127640/2754027
 */
public class PersonManageServiceActivity extends AppCompatActivity {
    EditText et_name;
    EditText et_age;
    EditText et_id;
    TextView tv_bind;
    TextView tv_set;
    TextView tv_get;
    private PersonMannageInterface mPersonMannageInterface;
    private boolean mBindServiceSuccess;
    private String mUUid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        PersonBean personBean = new PersonBean("测试", 20, "1000_1000l");
        PersonBean personBean2 = new PersonBean("测试", 20, "1000_1000l");

        /*

            java比较两个对象是否相等
            https://www.cnblogs.com/expiator/p/7813247.html

            java比较两个对象,可以通过重写equals()实现。

            1.比较对象时， ==比较的是地址，而equals()比较的是对象内容

            2.重写equals()方法必须重写hashCode()

         */

        if (personBean == personBean2) {
            Log.i("==", "personBean == personBean2");
        } else {
            //不管PersonBean类是否重写 hashCode 和 equals 方法 ，始终都会走 不等于， ==比较的是两个对象地址
            Log.i("==", "personBean 不等于 personBean2");
        }

        if (personBean.equals(personBean2)) {
            //如果PersonBean类重写 hashCode 和 equals 方法 ， personBean equals(personBean2)的返回结果 为 true
            Log.i("==", "personBean equals personBean2");
        } else {
            Log.i("==", "personBean 不 equals  personBean2");

        }
    }

    private void initView() {
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_id = findViewById(R.id.et_id);
        tv_bind = findViewById(R.id.tv_bind);
        tv_set = findViewById(R.id.tv_set);
        tv_get = findViewById(R.id.tv_get);
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    private void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        initEvent();
    }

    private void initEvent() {
        tv_bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBindServiceSuccess) {
                    bindPersonService();
                } else {
                    ToastUtils.show("不能重复绑定");

                    Log.i("==", "绑定 mBindServiceSuccess = " + mBindServiceSuccess + " 不能重复绑定");
                }

            }
        });

        tv_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });

        tv_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getName();
            }
        });
    }

    private void getName() {
        if (mPersonMannageInterface == null) {
            Log.i("==", "请先绑定");
            ToastUtils.show("请先绑定");
            return;
        }

        List<PersonBean> personBeans = null;
        try {
            personBeans = mPersonMannageInterface.getPersonBeans();
        } catch (RemoteException e) {
            e.printStackTrace();
            mBindServiceSuccess = false;
            Log.i("==", "客服端 获取异常 e: " + e.toString());
        }

        if (personBeans == null || personBeans.isEmpty()) {
            Log.i("==", "客服端 获取失败 personBeans == null || personBeans.isEmpty() ");
            ToastUtils.show("获取失败");

        } else {
            Log.i("==", "客服端 获取成功 personBeans.get(personBeans.size()-1) =  " + personBeans.get(personBeans.size() - 1).toString());
            ToastUtils.show("获取到的名字是：" + personBeans.get(personBeans.size() - 1).getName());
            return;
        }


//        try {
//            PersonBean personBeanById = mPersonMannageInterface.getPersonBeanById(mUUid);
//            if (personBeanById == null) {
//                Log.i("==", "获取失败  mUUid = " + mUUid);
//
//            } else {
//                Log.i("==", "获取成功 personBeanById =  " + personBeanById.toString());
//                ToastUtils.show("获取到的名字是：" + personBeanById.getName());
//            }
//        } catch (RemoteException e) {
//            e.printStackTrace();
//            ToastUtils.show("获取到的名字异常");
//            Log.i("==", "获取到的名字异常 e: " + e.toString());
//        }

    }

    private void add() {
        if (mPersonMannageInterface == null) {
            Log.i("==", "请先绑定");
            ToastUtils.show("请先绑定");
            return;
        }

        if (TextUtils.isEmpty(et_name.getText())) {
            ToastUtils.show("输入名字");
            return;
        }


        PersonBean personBean = new PersonBean();
        personBean.setName(et_name.getText().toString());
        if (TextUtils.isEmpty(et_age.getText().toString())) {
            personBean.setAge(0);
        } else {
            personBean.setAge(Integer.valueOf(et_age.getText().toString().trim()));
        }

        //UUID.randomUUID()生成唯一识别码 https://www.cnblogs.com/xiaohuizhenyoucai/p/11647547.html

        if (mUUid == null) {//这里 这样写为了测试 是否可添加重复的人员
            mUUid = UUID.randomUUID().toString();
        }
        personBean.setId(mUUid);
        Log.i("==", "add方法  personBean = " + personBean.toString());

        try {
            boolean addResult = mPersonMannageInterface.add(personBean);
            if (addResult) {
                Log.i("==", "添加成功  ");
                ToastUtils.show("添加成功");
            } else {
                Log.i("==", "添加失败  ");
                ToastUtils.show("添加失败");
            }

        } catch (RemoteException e) {
            e.printStackTrace();
            mBindServiceSuccess = false;
            ToastUtils.show("添加发生异常 ");
            Log.i("==", "添加发生异常 e: " + e.toString());

        }

    }

    /**
     * 注意:PersonService需要先在清单文件里声明，不然会绑定失败
     */
    private void bindPersonService() {
        Intent intent = new Intent(this, PersonManageService.class);
        mBindServiceSuccess = bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
        Log.i("==", "绑定 PersonManageService  mBindServiceSuccess = " + mBindServiceSuccess);
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("==", "绑定 PersonManageService onServiceConnected 已连接 ");

            mPersonMannageInterface = PersonMannageInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("==", "绑定 PersonManageService onServiceDisconnected 已断开");
        }
    };

    @Override
    protected void onDestroy() {
        if (mBindServiceSuccess) {
            unbindService(mServiceConnection);
            mBindServiceSuccess = false;
            Log.i("==", "onDestroy unbindService PersonManageService  解绑 mServiceConnection");
        } else {
            Log.i("==", "onDestroy 绑定 PersonManageService mBindServiceSuccess =  " + mBindServiceSuccess + "无需解绑");
        }
        super.onDestroy();
    }
}